# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: jaewon

import logging

from coin.exchange.base.order_gateway import OrderType
from coin.exchange.okex.kr_rest.product import OkexProduct
from coin.exchange.okex.order_gateway import OkexOrderGateway
from coin.exchange.okex_v3.order_gateway import OkexOrderGateway as OkexOrderGatewayV3
from coin.strategy.executor.passive.simple_mm_executor import \
  SimpleMMExecutor
from coin.strategy.executor.util.traits import (order_logger, SpotExecutorTraits)
from coin.strategy.executor.aggressive.aggressive_executor import AggressiveExecutor


class OkexSimpleTraits(SpotExecutorTraits):
  def __init__(self, executor_config, order_gateway, logger=None):
    logger = logger or logging.getLogger('OkexSimpleMMExecutor')
    super().__init__(executor_config, order_gateway, logger)

  def _submit_order_impl(self, product, price, qty, order_side, post_only=None):
    if not self.og.submittable():
      self._logger.warning('Skip submission: %s %f@%f' % (product, qty, price))
      return False

    self.og.submit(product, price, qty, order_side, OrderType.LIMIT, post_only=post_only)

  @order_logger
  def _submit_order(self, product, price, qty, order_side):
    self._submit_order_impl(product, price, qty, order_side)

  @order_logger
  def _submit_pass_order(self, product, price, qty, order_side):
    self._submit_order_impl(product, price, qty, order_side, self.config.post_only)

  def _cancel_orders_impl(self, order_id_list):
    self.og.cancel_multiple(order_id_list)


class OkexSimpleMMExecutor(SimpleMMExecutor):
  ProductType = OkexProduct
  OGType = OkexOrderGateway
  Traits = OkexSimpleTraits


class OkexSimpleAggExecutor(AggressiveExecutor):
  ProductType = OkexProduct
  OGType = OkexOrderGateway
  Traits = OkexSimpleTraits


class OkexSimpleMMExecutorV3(SimpleMMExecutor):
  ProductType = OkexProduct
  OGType = OkexOrderGatewayV3
  Traits = OkexSimpleTraits


class OkexSimpleAggExecutorV3(AggressiveExecutor):
  ProductType = OkexProduct
  OGType = OkexOrderGatewayV3
  Traits = OkexSimpleTraits
